package com.software.fanfan.utils;

import com.software.fanfan.exception.BusinessException;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateUtils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 日期时间工具方法
 *
 * Created by shiyanfei on 2018-01-09.
 */
public class DateUtil {

    /**
     * 格式 2015-08-07
     */
    public static final String DateFormat_yyyyMMdd = "yyyy-MM-dd";
    /**
     * 格式 15:25:30
     */
    public static final String DateFormat_HHmmss = "HH:mm:ss";

    /**
     * 格式 15:25
     */
    public static final String DateFormat_HHmm = "HH:mm";

    /**
     * 格式 2015-08
     */
    public static final String DateFormat_yyyyMM = "yyyy-MM";

    /**
     * 格式 2015-08-07 11:44:18
     */
    public static final String DateFormat_yyyyMMddHHmmss = "yyyy-MM-dd HH:mm:ss";

    /**
     * 格式 2015-08-07 11:44:18:591
     */
    public static final String DateFormat_yyyyMMddHHmmssSSS = "yyyy-MM-dd HH:mm:ss.SSS";

    /**
     * 格式 150807114418
     */
    public static final String DateFormat_yyMMddHHmmss = "yyMMddHHmmss";

    /**
     * 格式 20140416142030
     */
    public static final String DateFormat_TimeStemp_yyyyMMddHHmmss = "yyyyMMddHHmmss";

    /**
     * 格式 20150807114418591
     */
    public static final String DateFormat_TimeStemp = "yyyyMMddHHmmssSSS";

    /**
     * 格式 2015-08-13T16:49:02.039+0800
     */
    public static final String DateFormat_yyyyMMddTHHmmssSSSZ = "yyyy-MM-dd'T'HH:mm:ss.SSSZ";

    /**
     * 格式 2015-11-09T00:00:08.000+08:00
     */
    public static final String DateFormat_yyyyMMddTHHmmssSSSXXX = "yyyy-MM-dd'T'HH:mm:ss.SSSXXX";


    /**
     * 日期转化为字符串
     *
     * @param format
     * @return
     */
    public static String parseToString(Date date, String format) {
        DateFormat df = new SimpleDateFormat(format);
        String strDate = df.format(date);
        return strDate;
    }

    /**
     * 字符串转化为日期
     *
     * @param str
     * @param format
     * @return
     */
    public static Date parseToDate(String str, String format) {
        DateFormat df = new SimpleDateFormat(format);
        Date date = null;
        try {
            date = df.parse(str);
        } catch (ParseException e) {
            throw new BusinessException(e);
        }
        return date;
    }


    /**
     * 格式化日期
     */
    public static Date getFormatDate(Date date, String format) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat(format);
            String strDate = sdf.format(date);
            Date newDate = sdf.parse(strDate);
            return newDate;
        } catch (Exception e) {
            return null;
        }
    }

    public static long compareDate(Date date1, Date date2) {
        long l = date2.getTime() - date1.getTime();
        return l / (1000 * 60);
    }

    /**
     * 将字符串转为时间戳
     *
     * @param time
     * @return
     */
    public static String getTime(String time) throws ParseException {
        String re_time = null;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date d = sdf.parse(time);
        long l = d.getTime() / 1000;
        re_time = String.valueOf(l);
        return re_time;
    }

    /**
     * 将时间戳转为字符串
     *
     * @param time
     * @return
     */
    public static String getStrTime(String time) {
        String re_StrTime = null;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        long lcc_time = Long.valueOf(time);
        re_StrTime = sdf.format(new Date(lcc_time));
        return re_StrTime;
    }

    /**
     * 将时间戳转为字符串
     *
     * @param time
     * @return
     */
    public static String getStrDate(String time) {
        String re_StrTime = null;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        long lcc_time = Long.valueOf(time);
        re_StrTime = sdf.format(new Date(lcc_time));
        return re_StrTime;
    }

    /**
     * 格式化时间
     *
     * @param date
     * @return
     */
    public static String dateTimeToStr(Date date){
        if(date != null){
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            return sdf.format(date);
        }else {
            return null;
        }
    }

    /**
     * String转date
     *
     * @param dateStr
     * @return
     */
    public static Date strToDate(String dateStr) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return sdf.parse(dateStr);
    }

    /**
     * 格式化时间
     *
     * @param date
     * @return
     */
    public static String dateToStr(Date date) {
        if (date != null) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            return sdf.format(date);
        } else {
            return null;
        }
    }

    /**
     * 格式化时间
     *
     * @param date
     * @return
     */
    public static String dateToYyyyMMddHHmmssStr(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        return sdf.format(date);
    }

    /**
     * 验证日期格式为yyyy-MM-dd
     *
     * @param date
     * @return
     */
    public static boolean checkDate(String date) {
        String eL = "[0-9]{4}-[0-9]{2}-[0-9]{2}";
        Pattern pattern = Pattern.compile(eL);
        Matcher matcher = pattern.matcher(date);
        return matcher.matches();
    }

    public static boolean cheakDateStr(String date) {
        String eL = "^[1-9]\\d{3}-(0?[1-9]||1[0-2])-(0?[1-9]||[1-2][0-9]||3[0-1])$";
        Pattern pattern = Pattern.compile(eL);
        Matcher matcher = pattern.matcher(date);
        return matcher.matches();
    }

    public static boolean checkTime(String time) {
        String eL = "^(0?[1-9]|1\\d|2[0-3])(:(0?[0-9]|[1-5]\\d)){2}$";
        Pattern pattern = Pattern.compile(eL);
        Matcher matcher = pattern.matcher(time);
        return matcher.matches();
    }

    public static Date getCurrentEndDate() {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);

        return calendar.getTime();
    }

    public static Date getCurrentStartDate() {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);

        return calendar.getTime();
    }

    public static Calendar calculateDate(Date date, int field, int amount) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(field, amount);
        return calendar;
    }

    /*
    * 计算当天剩余毫秒数
    * */
    public static long remainingTime() {
        return 24 * 60 * 60 * 1000 - DateUtils.getFragmentInMilliseconds(Calendar.getInstance(), Calendar.DATE);
    }

    /**
     * 验证两个时间的时间范围是否在31天内
     *
     * @param firstTime
     * @param lastTime
     * @return
     * @throws ParseException
     */
    public static boolean checkTimeInMonth(String firstTime, String lastTime) throws ParseException {
        //对导出的时间段进行验证
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        if (StringUtils.isNotEmpty(firstTime) && StringUtils.isNotEmpty(lastTime)) {
            Date firstDate = format.parse(firstTime);
            Date lastDate = format.parse(lastTime);
            long devDate = firstDate.getTime() - lastDate.getTime();
            long devTime = devDate / (24 * 3600000);
            if (devTime <= 31) {
                return true;
            }
        }
        return false;
    }

}
